package cn.rslee.demo.spring.mybatis.entity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

/**
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2016年6月3日      325175         Create
 * ****************************************************************************
 * </pre>
 * @see http://programmers.stackexchange.com/questions/138391/should-i-use-the-date-type-in-jax-rs-pathparam
 * @author 325175
 * @since 1.0
 */
public class DateParam {

	private final Date date;

	public DateParam(String dateStr) throws WebApplicationException {
		if (isEmpty(dateStr)) {
			this.date = null;
			return;
		}
		final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			this.date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			throw new WebApplicationException(Response
					.status(Status.BAD_REQUEST)
					.entity("Couldn't parse date string: " + e.getMessage())
					.build());
		}
	}

	public Date getDate() {
		return date;
	}
	
	private boolean isEmpty(String str){
		return str == null || "".equals(str.trim());
	}
}
